// pages/goods/goods.js
const {
  getgoodsdetail,
  getgoodsrelated,
  addcart,
  getgoodsnumber
} = require("../../api/api.js")
const {checkLogin}=require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    attrisshow: false,
    id: 0, //商品id
    info: {}, //商品详情
    attribute: [], //规格参数
    brand: [], //制造商信息
    comment: [], //评论
    issue: [], //常见问题
    productList: [], //不同规格不同的价格
    specificationList: [], //规格列表
    content: '', //富文本
    checkedtext: "规格数量选择", //商品基本信息下的选择规格
    relategoods: [], //相关商品
    checkedSpecPrice: 0, //显示的商品价格
    prices: 0, //基础价格
    tmpSpecText: "请选择规格数量",
    number: 1, //商品的数量
    specification: "", //拼接字符串用 规格
    colors: "", //颜色
    attrimg: "", //弹出规格框img
    productid:"",
    cartgoodsnumber:"" //购物车商品数量
  },
  //点击规格数量选择
  switchpop() {
    this.setData({
      attrisshow: true
    })
  },
  //点击关闭规格数量选择
  closeAttr() {
    this.setData({
      attrisshow: false
    })
  },
  // 选择规格
  clickSkuValue(e) {
    console.log(e);
    let n = e.target.dataset.name
    let val = e.target.dataset.vitem.value
    let ind = e.target.dataset.index
    if (n == "规格") {
      this.setData({
        specification: val,
      })
      this.setData({
        tmpSpecText: this.data.specification + this.data.colors,
        checkedtext: this.data.specification + this.data.colors
      })
      this.computeprice(this.data.specification, this.data.colors)
      if (this.data.prices) {
        // console.log(this.data.prices);
        let price = this.data.prices * this.data.number
        this.setData({
          checkedSpecPrice: price
        })
      }
      let newvalist= this.data.specificationList[0].valueList.map((item, index) => {
        if (index == ind) {
          item.deleted=true
          return item
        }else{
          item.deleted=false
          return item
        }
      })
      this.setData({
        "specificationList[0].valueList":newvalist
      })
    } else {
      this.setData({
        colors: val,
        attrimg: e.target.dataset.vitem.picUrl
      })
      this.setData({
        tmpSpecText: this.data.specification + this.data.colors,
        checkedtext: this.data.specification + this.data.colors
      })
      this.computeprice(this.data.specification, this.data.colors)
      if (this.data.prices) {
        let price = this.data.prices * this.data.number
        this.setData({
          checkedSpecPrice: price
        })
      }
      let newvalist= this.data.specificationList[1].valueList.map((item, index) => {
        if (index == ind) {
          item.deleted=true
          return item
        }else{
          item.deleted=false
          return item
        }
      })
      this.setData({
        "specificationList[1].valueList":newvalist
      })
    }

  },
  computeprice(head, fool) {
    if (head && fool) {
      // console.log("都有值");
      let ind
      this.data.productList.map((item, index) => {
        if (item.specifications[0] == head && item.specifications[1] == fool) {
          ind = index
        }
      })
      let price = this.data.productList[ind].price
      let id=this.data.productList[ind].id
      this.setData({
        checkedSpecPrice: price,
        prices: price,
        productid:id
      })
    }
  },

  //点击减号
  cutNumber() {
    let n = this.data.number - 1
    if (n > 0) {
      this.setData({
        number: n
      })
      let price = this.data.prices * this.data.number
      this.setData({
        checkedSpecPrice: price
      })
    }
  },
  //点击加号
  addNumber() {
    let n = this.data.number + 1
    this.setData({
      number: n
    })
    let price = this.data.prices * this.data.number
    this.setData({
      checkedSpecPrice: price
    })
  },
  getrelateddata() {
    getgoodsrelated(this.data.id).then(res => {
      console.log(res);
      if (res.errno == 0) {
        this.setData({
          relategoods: res.data.goodsList
        })
      }
    })
  },
  //点击加入购物车
  onClickadd(){
    if(this.data.attrisshow==false){
      this.setData({
        attrisshow:true
      })
    }else{
      if(this.data.specification){
        let data={
          goodsId:this.data.id,
          number:this.data.number,
          productId:this.data.productid?this.data.productid:this.data.specificationList[0].valueList[0].id
        }
        addcart(data).then(res=>{
          console.log(res);
          if(res.errno==0){
            wx.showToast({
              title: '添加成功',
              icon:"success"
            })
            this.getcartnumber()
          }else{
            wx.showToast({
              title: res.errmsg,
              icon:"error"
            })
          }
        })
      }else{
        wx.showToast({
          title: '请选择规格颜色',
          icon: 'error',
          duration: 1500
        })
      }
    }
  },
  gotocart(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  // 获取商品数量
  getcartnumber(){
    console.log("获取商品数量");
    getgoodsnumber().then(res=>{
      if(res.errno==0){
        this.setData({
          cartgoodsnumber:res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad(options) {
    if (options.id) {
      this.setData({
        id: parseInt(options.id)
      })
      getgoodsdetail(options.id).then(res => {
        if (res.errno == 0) {
          const {
            info,
            attribute,
            brand,
            comment,
            issue,
            productList,
            specificationList
          } = res.data
          let num = info.retailPrice * this.data.number
          this.setData({
            info: info,
            attribute: attribute,
            brand: brand,
            comment: comment,
            issue: issue,
            productList: productList,
            specificationList: specificationList,
            checkedSpecPrice: num,
            attrimg: info.picUrl,
            prices: num,
            //富文本处理
            content: info.detail.replace(/\<img/gi, '<img class="richimg"')
          })
        }

      })
    }
    this.getrelateddata()
    checkLogin().then(res=>{
      if(res==true){
        return
      }
    }).catch(res=>{
      if(res==false){
        wx.showToast({
          title: '请先登录',
          icon:'error',
          duration: 1000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/auth/login/login',
          })
        }, 1300);
      }
    })
    this.getcartnumber()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})